<?php
class skill extends Model {
	/**
	 * 获取解题技巧
	 *
	 * @param 科目id $sub_id
	 *        	如果是0则此参数无效
	 * @param 父技巧id $p_id
	 *        	默认为0（0为跟节点）
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getSkillFromSubId($sub_id, $p_id = 0) {
		if ($sub_id == 0) {
			$sql = "SELECT * FROM  `skill` WHERE  `parent_id` ='" . $p_id . "' ";
		} else {
			$sql = "SELECT * FROM  `skill` WHERE  `parent_id` =" . $p_id . " AND  `sub_id` =" . $sub_id;
		}
		// echo $sql;
		return $this->fetchAll ( $sql );
	}
	public function searchSkill($keyword, $sub_id) {
		$sql = "SELECT *  FROM  `skill` WHERE  `skill_name` LIKE  '%" . $keyword . "%' AND  `sub_id` = " . $sub_id;
		return $this->fetchAll ( $sql );
	}
	
	/**
	 * 删除解题技巧
	 *
	 * @param unknown_type $skill_id        	
	 * @return -1还有子类不能删除 , 0 :失败, 1: 成功
	 */
	public function delSkill($skill_id) {
		if ($this->getSkillFromSubId ( 0, $skill_id )) {
			return - 1;
		} else {
			$sql = "DELETE FROM `howdo`.`skill` WHERE `skill`.`skill_id` = " . $skill_id;
			return $this->del ( $sql ) ? 1 : 0;
		}
	}
	
	/**
	 * 添加解题技巧
	 *
	 * @param unknown_type $sub_id        	
	 * @param unknown_type $p_id        	
	 * @param unknown_type $user_id        	
	 * @param unknown_type $title        	
	 * @param unknown_type $content        	
	 * @return lastid false;
	 */
	public function addSkill($sub_id, $p_id, $user_id, $title, $content = "") {
		$sql = "INSERT INTO `skill` (`skill_id` ,`skill_name` ,`parent_id` ,`sub_id` ,`skill_create_admin_id` ,`skill_cont` ,`skill_create_time`)
				VALUES (NULL ,  '" . $title . "',  '" . $p_id . "',  '" . $sub_id . "',  '" . $user_id . "',  '" . $content . "', NOW( ) ) ";
		// echo $sql;
		return $this->insert ( $sql );
	}
	
	/**
	 * 根据id获取一条数据
	 *
	 * @param unknown_type $skillid        	
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getSkillFromId($skillid) {
		$sql = "SELECT * FROM  `skill` WHERE  `skill_id` =" . $skillid . " LIMIT 0 , 1";
		return $this->fetchRow ( $sql );
	}
	public function modifySkill($id, $arr) {
		if ($arr) {
			reset ( $arr );
			list ( $key, $val ) = each ( $arr );
			$str = " `" . $key . "` = '" . $val . "' ";
			while ( list ( $key, $val ) = each ( $arr ) ) {
				// echo "$key => $val\n";
				if ($val) {
					$str .= " , `" . $key . "` = '" . $val . "' ";
				} else {
					$str .= " , `" . $key . "` = NULL ";
				}
			}
			$sql = "UPDATE `skill` SET " . $str . " WHERE `skill_id` =" . $id . "  LIMIT 1";
			// echo $sql;
			return $this->update ( $sql );
		} else {
			return false;
		}
	}
	
	/**
	 * 获取解题技巧列表
	 *
	 * @author linkai
	 * @version 2013-11-27 下午5:19:42
	 * @return $list
	 */
	public function getSkillList($subIDList,$fatherID) {
		if ($subIDList == - 1) {
			$subID = "";
		} else {
			$subIDs = split ( "_", $subIDList );
			$subID = "WHERE `parent_id`=$fatherID AND `sub_id`=$subIDs[0] ";
			for($i = 1; $i < count ( $subIDs ); $i ++) {
				$subID .= "OR `sub_id`=$subIDs[$i] ";
			}
		}
		$sql = "SELECT `skill_id`,`skill_name`
				FROM `skill`";
		$sql .= $subID . "ORDER BY `skill_id` ASC ";
		return $this->fetchAll ( $sql );
	}
	
	/**
	 * 判重
	 *
	 * @param unknown_type $skillName        	
	 * @param unknown_type $subId        	
	 * @param unknown_type $skillId        	
	 * @return Ambigous <boolean, multitype:>
	 */
	public function check($skillName, $subId, $skillId = 0) {
		if ($skillId) {
			$sql = "SELECT * FROM `skill` WHERE `skill_name` = '" . $skillName . "' AND `sub_id` = '" . $subId . "' AND `skill_id` != '" . $skillId . "'";
		} else {
			$sql = "SELECT * FROM `skill` WHERE `skill_name` = '" . $skillName . "' AND `sub_id` = '" . $subId . "'";
		}
		
		return $this->fetchAll ( $sql );
	}
}